package com.example.zbh.dsliao.thread;

import java.util.Hashtable;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by zbh on 15/12/7.
 */
public class ThreadPool {
    private ExecutorService pool;
    private Hashtable<String, Runnable> hashtable;
    private static ThreadPool threadPool;
    public static ThreadPool getInstance(){
        if (threadPool == null){
            threadPool = new ThreadPool();
        }
        return threadPool;
    }

    private ThreadPool() {
        hashtable = new Hashtable<>();
        pool = Executors.newCachedThreadPool();
    }
    public void addThread(String s, Runnable runnable){
        hashtable.put(s, runnable);
    }
    public void executeThread(String s){
        Runnable runnable = hashtable.get(s);
        if (runnable != null){
            pool.execute(runnable);
        }
    }
    public void shutDownPool(){
        pool.shutdown();
    }

}
